﻿using System;
using System.Threading.Tasks;
using Looptribe.Moodlin8.Model;
using Windows.Foundation;
using Windows.UI.Popups;
using Windows.UI.StartScreen;

namespace Looptribe.Moodlin8.UIHelper
{
    public static class SecondaryTileManager
    {
        private const string TileIdPrefix = "Moodlin8Course.";

        public static bool CourseExists(long courseId)
        {
            return SecondaryTile.Exists(GetCourseTileId(courseId));
        }

        public static string GetCourseTileId(long courseId)
        {
            return TileIdPrefix + courseId.ToString();
        }

        public static async Task<bool> PinCourse(Course course, Rect rect)
        {
            var logo = new Uri("ms-appx:///Assets/SecTile_sm.png");
            var secondaryTile = new SecondaryTile(GetCourseTileId(course.Id),
                course.Shortname, course.Shortname, course.Id.ToString(), TileOptions.ShowNameOnLogo, logo);
            secondaryTile.SmallLogo = new Uri("ms-appx:///assets/moodle_tile_sm.png");
            var isPinned = await secondaryTile.RequestCreateForSelectionAsync(rect, Placement.Above);
            return isPinned;
        }

        public static async Task<bool> UnpinCourse(long courseId, Rect rect)
        {
            var tileId = GetCourseTileId(courseId);
            var secondaryTile = new SecondaryTile(tileId);
            var isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync(rect, Placement.Above);
            return isUnpinned;
        }
    }
}
